package com.ryskoo.customer.customerintegral.repository;

import com.ryskoo.catering.constant.HCLCacheConstant;
import com.ryskoo.customer.customerintegral.domain.CustomerIntegralPO;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;

/**
 * @author BeHappy
 */
@Primary
@Repository
public class CustomerIntegralRedisImpl extends BasicRedisImpl<CustomerIntegralPO> implements ICustomerIntegralRepository {

    private static final String cacheKey = HCLCacheConstant.HCL_INTEGRAL_KEY;
    @Autowired
    private RedisUtil redisClient;
    @Autowired
    private ICustomerIntegralRepository customerIntegralRepository;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(customerIntegralRepository);
    }

    public CustomerIntegralRedisImpl() {
        super(cacheKey);
    }

    @Override
    public void create(CustomerIntegralPO m) {
        super.create(m);
        redisClient.setObject(cacheKey + m.getCustomerUuid() + "_" + m.getVendorUuid(), m);
    }

    @Override
    public void update(CustomerIntegralPO m) {
        super.update(m);
        redisClient.setObject(cacheKey + m.getCustomerUuid() + "_" + m.getVendorUuid(), m);
    }

    /**
     * 获取会员积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @return
     */
    @Override
    public CustomerIntegralPO getIntegral(String customerUuid, String vendorUuid) {
        CustomerIntegralPO m = (CustomerIntegralPO) redisClient.getObject(cacheKey + customerUuid + "_" + vendorUuid, CustomerIntegralPO.class);
        if(null != m){
            return m;
        }
        m = customerIntegralRepository.getIntegral(customerUuid, vendorUuid);
        if(null != m){
            redisClient.setObject(cacheKey + customerUuid + "_" + vendorUuid, m);
        }
        return m;
    }
}
